﻿/*
 * Copyright 2010 Pieter De Rycke
 *
 * This file is part of Frozen Bubble XNA.
 *
 * Frozen Bubble XNA is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * Frozen Bubble XNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Frozen Bubble XNA. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * XNA and Windows Phone 7 port:
 *    Pieter De Rycke
 *
 * Websites:
 *    http://www.frozen-bubble.org/
 *    http://pieterderycke.wordpress.com/
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace FrozenBubble
{
    class CheckableMenuEntry : MenuEntry
    {
        private bool _checked;

        public CheckableMenuEntry(string text, Vector2 position, Texture2D image, Texture2D hoverOver,
            Texture2D imageChecked, Texture2D hoverOverChecked, bool selected)
        {
            Text = text;
            Position = position;
            Image = image;
            HoverOver = hoverOver;
            ImageChecked = imageChecked;
            HoverOverChecked = hoverOverChecked;

            if (selected)
                Texture = HoverOver;
            else
                Texture = Image;
        }

        public CheckableMenuEntry(string text, Vector2 position, Texture2D image, Texture2D hoverOver,
            Texture2D imageChecked, Texture2D hoverOverChecked)
            : this(text, position, image, hoverOver, imageChecked, hoverOverChecked, false)
        {
        }

        #region Public Properties

        public Texture2D ImageChecked { get; private set; }
        public Texture2D HoverOverChecked { get; private set; }

        public bool IsChecked 
        {
            get
            {
                return _checked;
            }
            set
            {
                _checked = value;
                UpdateTexture();
            }
        }

        #endregion

        internal override void DoClick(object sender)
        {
            IsChecked = !IsChecked;
            base.DoClick(sender);
        }

        internal override void DoHover(object sender)
        {
            IsSelected = true;
            UpdateTexture();
        }

        internal override void DoUnHover(object sender)
        {
            IsSelected = false;
            UpdateTexture();
        }

        #region Private Methods

        private void UpdateTexture()
        {
            if (IsChecked)
            {
                if (IsSelected)
                    Texture = HoverOverChecked;
                else
                    Texture = ImageChecked;
            }
            else
            {
                if (IsSelected)
                    Texture = HoverOver;
                else
                    Texture = Image;
            }
        }

        #endregion
    }
}
